package com.sky.service.impl;

import com.sky.constant.JwtClaimsConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;


@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

	@Autowired
	private ShoppingCartMapper shoppingCartMapper;
	@Autowired
	private DishMapper dishMapper;
	@Autowired
	private SetmealMapper setmealMapper;

	/**
	 * 添加商品到购物车
	 */
	@Override
	public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {

		//属性拷贝到实体类中
		ShoppingCart shoppingCart = new ShoppingCart();
		BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
		shoppingCart.setUserId((Long) BaseContext.get(JwtClaimsConstant.USER_ID));

		//查询数据库判断是否已经存在
		List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

		// 已存在则修改原记录的数量字段
		if (shoppingCartList != null && shoppingCartList.size() > 0) {
			ShoppingCart cart = shoppingCartList.get(0);
			cart.setNumber(cart.getNumber() + 1);
			shoppingCartMapper.updateNumberById(cart);
		} else {
			//购物车中不存在要添加的商品，则新增一条记录
			//判断添加到购物车中的是菜品还是套餐
			Long dishId = shoppingCartDTO.getDishId();
			if (dishId != null) {
				Dish dish = dishMapper.getById(dishId);
				shoppingCart.setName(dish.getName());
				shoppingCart.setImage(dish.getImage());
				shoppingCart.setAmount(dish.getPrice());
			} else {
				Setmeal setmeal = setmealMapper.getbyId(shoppingCartDTO.getSetmealId());
				shoppingCart.setName(setmeal.getName());
				shoppingCart.setImage(setmeal.getImage());
				shoppingCart.setAmount(setmeal.getPrice());
			}
			shoppingCart.setNumber(1);
			shoppingCart.setCreateTime(LocalDateTime.now());
			shoppingCartMapper.insert(shoppingCart);
		}

	}

	/**
	 * 查询购物车中的商品
	 *
	 * @return
	 */
	@Override
	public List<ShoppingCart> list() {
		//获取当前用户id
		Long id = (Long) BaseContext.get(JwtClaimsConstant.USER_ID);
		ShoppingCart shoppingCart = ShoppingCart.builder().userId(id).build();
		List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
		return list;

	}

	/**
	 * 清空购物车
	 */
	@Override
	public void clean() {
		//获取当前用户id
		Long id = (Long) BaseContext.get(JwtClaimsConstant.USER_ID);
		shoppingCartMapper.deleteByUserId(id);
	}

	/**
	 * 删除购物车中的一个商品
	 *
	 * @param shoppingCartDTO
	 */
	@Override
	public void delete(ShoppingCartDTO shoppingCartDTO) {
		//属性拷贝到实体类中
		ShoppingCart shoppingCart = new ShoppingCart();
		BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
		shoppingCart.setUserId((Long) BaseContext.get(JwtClaimsConstant.USER_ID));
		//得到其数据库中实体类
		List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

		if (shoppingCartList != null && shoppingCartList.size() > 0) {
			ShoppingCart cart = shoppingCartList.get(0);
			if (cart.getNumber()==1){
				shoppingCartMapper.deleteById(cart);
			}else{
				cart.setNumber(cart.getNumber() - 1);
				shoppingCartMapper.updateNumberById(cart);
			}

		}
	}
}
